//
// Created by XiongYingqi on 14-5-22.
// Copyright (c) 2014 XiongYingqi. All rights reserved.
//

#import "KRMainTabBar.h"
#import "KRContactsViewController.h"
#import "KRChatViewController.h"
#import "RDVTabBarController.h"
#import "RDVTabBarItem.h"
#import "XYQDrawPersonImage.h"
#import "XYQImageHelper.h"


@implementation KRMainTabBar {

}


#pragma mark - Methods

- (UIViewController *)setupViewControllers {
    UIViewController *contactsViewController = [[KRContactsViewController alloc] init];
    UIImage *contactsImage = [UIImage imageNamed:@"person.png"];


    UIViewController *contactsNavigationController = [[UINavigationController alloc]
            initWithRootViewController:contactsViewController];


    UIViewController *conversationViewController = [[KRChatViewController alloc] init];
    UIImage *conversationImage = [UIImage imageNamed:@"recent.png"];

    UIViewController *conversationNavigationController = [[UINavigationController alloc]
            initWithRootViewController:conversationViewController];

    UIViewController *historyViewController = [[KRChatViewController alloc] init];
    UIImage *historyImage = [UIImage imageNamed:@"history.png"];

    UIViewController *historyNavigationController = [[UINavigationController alloc]
            initWithRootViewController:historyViewController];


    RDVTabBarController *tabBarController = [[RDVTabBarController alloc] init];
    [tabBarController setViewControllers:@[contactsNavigationController, conversationNavigationController, historyNavigationController]];
    tabBarController.delegate = self;
    tabBarController.tabBar.delegate = self;

    NSArray *items = tabBarController.tabBar.items;
    RDVTabBarItem *item1 = [items objectAtIndex:0];

//    [item1 setImage:contactsImage];
    UIColor *backGroundColor = [XYQColor defaultColor];
    [item1 setFinishedSelectedImage:[contactsImage imageWithTintColor:backGroundColor] withFinishedUnselectedImage:contactsImage];

//    [item1 setBackgroundColor:[UIColor colorWithRed:6 green:122 blue:181 alpha:0.6]];
//    [item1 setTintColor:[UIColor colorWithRed:6/255 green:122/255 blue:181/255 alpha:0.6]];
//    [item1 setBackgroundSelectedImage:[XYQDrawPersonImage getDefaultImage] withUnselectedImage:nil];
    RDVTabBarItem *item2 = [items objectAtIndex:1];
    [item2 setFinishedSelectedImage:[conversationImage imageWithTintColor:backGroundColor] withFinishedUnselectedImage:conversationImage];

//    [item2 setBackgroundSelectedImage:[XYQDrawPersonImage getDefaultImage] withUnselectedImage:nil];
    RDVTabBarItem *item3 = [items objectAtIndex:2];
    [item3 setFinishedSelectedImage:[historyImage imageWithTintColor:backGroundColor] withFinishedUnselectedImage:historyImage];

//    [item3 setBackgroundSelectedImage:[XYQDrawPersonImage getDefaultImage] withUnselectedImage:nil];

//    self.viewController = tabBarController;

    item1.title = @"联系人";
    item2.title = @"会话";
    item3.title = @"历史消息";

    [self setTitleAttr:item1];
    [self setTitleAttr:item2];
    [self setTitleAttr:item3];


    [self customizeTabBarForController:tabBarController];
    return tabBarController;
}

- (void)setTitleAttr:(RDVTabBarItem *)item {

    //NSFont = "<UICTFont: 0xa613300> font-family: \".HelveticaNeueInterface-Regular\"; font-weight: normal; font-style: normal; font-size: 12.00pt";

//    UIFont *font = [UIFont systemFontOfSize:12];
//
//    NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
//    [attributes setObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
//    [attributes setObject:font forKey:NSFontAttributeName];
////    [attributes setObject:@"" forKey:NSTit];
//    [item setSelectedTitleAttributes:attributes];
//    [item setUnselectedTitleAttributes:attributes];
    [item setTintColor:[XYQColor defaultColor]];
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    RDVTabBarItem *item = tabBarController.tabBar.selectedItem;
    NSLog(@"item ======== %@", item);
}


- (void)customizeTabBarForController:(RDVTabBarController *)tabBarController {
//    UIImage *finishedImage = [UIImage imageNamed:@"tabbar_selected_background"];
//    UIImage *unfinishedImage = [UIImage imageNamed:@"tabbar_normal_background"];
//    NSArray *tabBarItemImages = @[@"first", @"second", @"third"];
//
//    NSInteger index = 0;
//    for (RDVTabBarItem *item in [[tabBarController tabBar] items]) {
//        [item setBackgroundSelectedImage:finishedImage withUnselectedImage:unfinishedImage];
//        UIImage *selectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_selected",
//                                                                                [tabBarItemImages objectAtIndex:index]]];
//        UIImage *unselectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_normal",
//                                                                                  [tabBarItemImages objectAtIndex:index]]];
//        [item setFinishedSelectedImage:selectedimage withFinishedUnselectedImage:unselectedimage];
//
//        index++;
//    }


}


- (void)customizeInterface {
    UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];

    UIImage *backgroundImage = nil;
    NSDictionary *textAttributes = nil;

    if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
        backgroundImage = [UIImage imageNamed:@"navigationbar_background_tall"];

        textAttributes = @{
                NSFontAttributeName : [UIFont boldSystemFontOfSize:18],
                NSForegroundColorAttributeName : [UIColor blackColor],
        };
    } else {
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
        backgroundImage = [UIImage imageNamed:@"navigationbar_background"];

        textAttributes = @{
                           UITextAttributeFont: [UIFont boldSystemFontOfSize:18],
                           UITextAttributeTextColor: [UIColor blackColor],
                           UITextAttributeTextShadowColor: [UIColor clearColor],
                           UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetZero],
                           };
#endif
    }

    [navigationBarAppearance setBackgroundImage:backgroundImage
                                  forBarMetrics:UIBarMetricsDefault];
    [navigationBarAppearance setTitleTextAttributes:textAttributes];
}

- (BOOL)tabBar:(RDVTabBar *)tabBar shouldSelectItemAtIndex:(NSInteger)index1 {
    RDVTabBarItem *item = tabBar.selectedItem;
    NSLog(@"shouldSelectItemAtIndex item ======== %@", item);
    return YES;
}

- (void)tabBar:(RDVTabBar *)tabBar didSelectItemAtIndex:(NSInteger)index1 {
//    NSArray *items = tabBar.items;
//    for(RDVTabBarItem *item in items){
//        item.tintColor = [UIColor blackColor];
//    }
//    RDVTabBarItem *item = tabBar.selectedItem;
////    item.backgroundColor = [XYQColor defaultColor];
////    item.title = @"选择";
////    [item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],UITextAttributeTextColor, nil] forState:UIControlStateNormal];
//
//
//    NSLog(@"didSelectItemAtIndex item ======== %@", item);
}


@end